const app = getApp();
import { areaList } from '@vant/area-data';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userName: '', // 收货人名字
    mobile: '', //手机号
    province: '', //省份名称
    chengshi: '', //城市名称
    districts: '', // 区县名称
    site: '', //详细地址
    remark: '',// 备注
    // GOODS_NAME:'',//粽子名字
    city:false,
    areaList,
  },


  //验证手机号
  mobileInput(e) {
    let value = e.detail.value.replace(/\D/g, '')
    this.setData({
      mobile: value,
    })
    var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(19[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (this.data.mobile.length == 0) {
      wx.showToast({
        title: '输入的手机号为空，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        mobileFormat: false,
      })
    } else if (this.data.mobile.length < 11) {

      wx.showToast({
        title: '手机号长度有误，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        STUDENT_PHONE: '',
        mobileFormat: false,
      })

    } else if (!myreg.test(this.data.mobile)) {
      wx.showToast({
        title: '手机号有误，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        STUDENT_PHONE: '',
        mobileFormat: false,
      })

    } else {
      this.setData({
        mobileFormat: true,
      })
    }
  },

  // 收货人姓名
  user(event) {
    console.log('username', event.detail.value)
    this.setData({
      userName: event.detail.value.trim()
    })
  },
  // 省份
  prov(event) {
    console.log('province', event.detail.value)
    this.setData({
      province: event.detail.value.trim()
    })
  },
  // 城市
  city(event) {
    console.log('province', event.detail.value)
    this.setData({
      chengshi: event.detail.value.trim()
    })
  },
  // 区县
  dist(event) {
    console.log('districts', event.detail.value)
    this.setData({
      districts: event.detail.value.trim()
    })
  },
  // 详细地址
  address(event) {
    console.log('districts', event.detail.value)
    this.setData({
      site: event.detail.value.trim()
    })
  },
  // 备注
  note(event) {
    console.log('remark', event.detail.value)
    this.setData({
      remark: event.detail.value.trim()
    })
  },

  submit() {
    const from = {
      CUSTOMER_TEL: this.data.mobile, //手机
      USER_ASSETS_NAME: this.data.userName, //收货人名字
      OPEN_ID: app.globalData.openid, //用户OPEN_ID
      PROVINCE_NAME: this.data.province, //省份名称
      CITY_NAME: this.data.chengshi, //城市名称
      DISTRICT_NAME: this.data.districts, //区县名称
      ADDRESS: this.data.site, //详细地址  
      // REMARKS: this.data.remark,//备注
      USER_COUPON_ID:this.data.USER_COUPON_ID,
      // GOODS_NAME:this.data.GOODS_NAME//粽子名称
    }

    if (app.globalData.openid && this.data.mobile && this.data.userName && this.data.province && this.data.chengshi && this.data.districts && this.data.site ) {
      wx.showLoading({
        title: '加载中...',
      })

      wx.request({
        url: app.globalData.interface+'/shangLuoWork/API_2071_saveShippingAddress',
        method: 'POST',
        data: from,
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log('res', res);
          console.log('res', res.data.result);
          wx.hideLoading()
          if (res.data.result == "success") {
            console.log('种子', res.data.data);

            wx.showModal({
              content: res.data.data,
              showCancel: false,
              success: function (res) {
                if (res.confirm) {//这里是点击了确定以后
                  wx.navigateBack({
                    delta: 2
                  })
                }
              }
            });
          } else {
            wx.showModal({
              content: res.data.data,
              showCancel: false,
              success: function (res) {
                if (res.confirm) {//这里是点击了确定以后
                  wx.navigateBack({
                    delta: 2
                  })
                }
              }
            });
          }
        },
        fail: (res => {
          wx.hideLoading()
        })
      })

    } else {
      wx.showModal({
        title: '信息不完善',
        content: '请完善表单信息',
        showCancel: false,
      });
    }
    console.log('from', from);
  },
  reset() {
    this.setData({
      userName: '', // 收货人名字
      mobile: '', //手机号
      province: '', //省份名称
      chengshi: '', //城市名称
      districts: '', // 区县名称
      site: '', //详细地址
      // remark: '' // 备注
    })
  },

  bindconfirm(e){
    console.log(e.detail.values[0].name);
    this.setData({
      province:e.detail.values[0].name, //省份名称
      chengshi: e.detail.values[1].name,//城市名称
      districts:e.detail.values[2].name, // 区县名称
    })

    this.setData({
      city:false
    })

  },

  bindcancel(){
    this.setData({
      city:false
    })
  },


  onClose() {
    this.setData({ city: false });
  },

  buttn_c() {
      this.setData({
        city:true
      })
  },





  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    console.log(e);
    this.setData({
      USER_COUPON_ID:e.data
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})